package org.microservice.system.controller;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.microservice.common.base.BaseControllerImpl;
import org.microservice.common.bean.UserVo;
import org.microservice.common.util.PageUtils;
import org.microservice.common.util.Query;
import org.microservice.common.util.R;
import org.microservice.common.util.constant.CommonConstant;
import org.microservice.system.dao.SysUserDao;
import org.microservice.system.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: 齐丹枫
 * Date: 2018/6/20
 * Time: 15:54
 * To change this template use File | Settings | File Templates.
 * Description:
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class SysUserController extends BaseControllerImpl<SysUserDao,UserVo> {

    @Autowired
    private SysUserService sysUserService;
    /**
     * 由用户名或用户id查询用户信息
     * @param userName
     * @return
     */
    @GetMapping("/getUserInfoByUserName")
    public R getUserInfoByUserName(@RequestParam(value = "userName") String userName){
        if(StringUtils.isBlank(userName)){
            return R.error("请携带查询条件");
        }
        UserVo name = sysUserService.getUserInfoByUserName(userName);
        return R.ok().put("data",name == null?new HashMap<>():name);

    }

    public R getListPage(@RequestBody Map<String,Object> params){
        Query query = new Query(params);

        List<UserVo> userVos = sysUserService.getUserListPage(query);

        int total = sysUserService.getUserListPageTotal(params);

        PageUtils pageUtils = new PageUtils(userVos, total, query.getLimit(), query.getCurrPage());

        return R.ok().put("data",pageUtils);
    }


    public R save(@RequestBody UserVo userVo){
        R r = sysUserService.save(userVo);
        return r;

    }



    public R update(@RequestBody UserVo userVo){
        R r = sysUserService.update(userVo);
        return r;

    }

    /**
     * 授予角色权限
      * @param userVo
     * @return
     */
    @PostMapping("/assignRoles")
    public R assignRoles(@RequestBody UserVo userVo){
        R r =sysUserService.assignRoles(userVo);
        return r;

    }

    /**
     * 授予数据权限
     * @param params
     * @return
     */
    @PostMapping("/assignDataScope")
    public R assignDataScope(@RequestBody Map<String,Object> params){
        String userName = (String) params.get("userName");
        int id = (Integer) params.get("id");
        Map<String,List<Integer>> dataScopes = (Map<String,List<Integer>>) params.get("dataScopes");
        R r =sysUserService.assignDataScope(userName,id,dataScopes);
        return r;

    }


    @GetMapping("/getDataScopeByUserIdOrUserName")
    public R getDataScopeByUserIdOrUserName(@RequestParam("id") int id,@RequestParam("userName") String userName){
        Map<String ,List<Integer>> dataScopes=  sysUserService.getDataScopeByUserIdOrUserName(id,userName);
        return R.ok().put("data",dataScopes);
    }


    /**
     * 禁用启用用户
     * @param userVo
     * @return
     */
    @PostMapping("/disabledOrEnabled")
    public R disabledOrEnabled(@RequestBody UserVo userVo){
        userVo.setDisabled(userVo.isDisabled()? CommonConstant.DISABLED:CommonConstant.ENABLED);
        boolean b = sysUserService.updateById(userVo);
        if(b){
            return R.ok();
        }
        return R.error();
    }


    /**
     * 更新密码
     * @param userVo
     * @return
     */
    @PostMapping("/updatePassword")
    public R updatePassword(@RequestBody UserVo userVo){

        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();

        userVo.setPassword(bCryptPasswordEncoder.encode(userVo.getPassword()));

        boolean b = sysUserService.updateById(userVo);
        if(b){
            return R.ok();
        }
        return R.error();
    }


    /**
     * 上传用户头像
     * @param multipartFile
     * @return
     */
    @GetMapping("/uploadAvatar")
    public R uploadAvatar(@RequestParam("file") MultipartFile multipartFile){
        System.out.println(multipartFile.getOriginalFilename());
        return R.ok();
    }

    /**
     * 根据部门id获取部门领导
     * @param departmentId
     * @return
     */
    @GetMapping("/getDepartmentLeadersByDepartmentId")
    public R getDepartmentLeadersByDepartmentId(@RequestParam("departmentId") int departmentId){
        List<Map<String,String>> userVos=  sysUserService.getDepartmentLeadersByDepartmentId(departmentId);
        return R.ok().put("data",userVos);
    }
}
